package com.vidyu.datalayer.dataaccesscomponents.impl;

import com.vidyu.businesslayer.businessentities.Contractor;
import com.vidyu.businesslayer.businessentities.Employer;
import com.vidyu.businesslayer.businessentities.MonitorEvent;
import com.vidyu.datalayer.dataaccesscomponents.ContractorDao;
import com.vidyu.datalayer.dataaccesscomponents.MonitorEventDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: eyalsa
 * Date: 28/02/12
 * Time: 14:38
 * Implementation Dao object for ContractorDao
 */

@SuppressWarnings({"unchecked", "UnusedDeclaration"})
@Repository("contractorDao")
public class ContractorDaoImpl extends BaseDaoImpl implements ContractorDao {

    @Autowired
    private HibernateTemplate template;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.NESTED)
    public long save(Contractor contractor){
        return ((Long) template.save(contractor));
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.NESTED)
    public void update(Contractor contractor){
        template.update(contractor);
    }


    @Override
    public Contractor findById(long id) {
        return template.get(Contractor.class, id);
    }

    @Override
    public Contractor getContractorByEmail(String email){
        List<Contractor> contractors = this.findByNamedQuery("getContractorByEmail", email);
        if (contractors.isEmpty()) {
            return null;
        }
        return contractors.get(0);
    }


    @Override
    public Contractor getContractorByEmailAndPassword(String email, String password){
        List<Contractor> contractors = this.findByNamedQuery("getContractorByEmailAndPassword", email, password);
        if (contractors.isEmpty()) {
            return null;
        }
        return contractors.get(0);
    }
}
